const fs = require('fs')
const fspromises = require('fs').promises
const path = require('path')

class Files {
    constructor(root) {
        this.root = root
    }
    read(filePath) {
        return fspromises.readFile(filePath, { encoding: 'utf8' })
    }
    readDir(dirPath) {
        return fspromises.readdir(dirPath, { encoding: 'utf8' })
    }
    isDir(filePath) {
        return fspromises.stat(filePath).then((stats) => {
            return stats.isDirectory()
        })
    }
    write(filePath, content) {

        return fspromises.writeFile(filePath, content, { encoding: 'utf8' })
    }
    writeDir(dirPath) {
        if(!fs.existsSync){
            return fspromises.mkdir(newDir, { encoding: 'utf8' })
        }else{
            console.log("文件夹已存在");
            
        }
       
    }
    rename(oldPath, newPath) {
        return fspromises.rename(oldPath, newPath)
    }
    delete(filePath) {
        return fspromises.unlink(filePath)
    }
    async deleteFloder(dirPath) {
        if (fs.existsSync(dirPath)) {
            const fileList = await this.readDir(dirPath)

            for (let index = 0; index < fileList.length; index++) {
                const file = fileList[index];
                const filePath = path.join(dirPath, file)
                const value = await this.isDir(filePath);
                if (value) {
                    await this.deleteFloder(filePath)
                } else {
                    await this.delete(filePath)
                }
            }
            fs.rmdirSync(dirPath)
        } else {
            console.log('文件路径不存在，请再次确认');
        }

    }
}

module.exports = Files;